package com.anyi.gulimall.product.service.impl;

import com.anyi.gulimall.common.utils.RRException;
import com.anyi.gulimall.product.GulimallProductApplication;
import com.anyi.gulimall.product.entity.BrandEntity;
import com.anyi.gulimall.product.entity.CategoryEntity;
import com.anyi.gulimall.product.service.BrandService;
import com.anyi.gulimall.product.service.CategoryService;
import com.anyi.gulimall.product.vo.BrandVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.anyi.gulimall.common.utils.PageUtils;
import com.anyi.gulimall.common.utils.Query;

import com.anyi.gulimall.product.dao.CategoryBrandRelationDao;
import com.anyi.gulimall.product.entity.CategoryBrandRelationEntity;
import com.anyi.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandService brandService;


    @Autowired
    private CategoryService categoryService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据品牌id获取所有的分类信息
     * @param brandId
     * @return
     */
    @Override
    public List<CategoryBrandRelationEntity> getBrandCategoryList(Long brandId) {
        List<CategoryBrandRelationEntity> list = this.list(
                new LambdaQueryWrapper<CategoryBrandRelationEntity>()
                        .eq(CategoryBrandRelationEntity::getBrandId, brandId));
        return list;
    }

    /**
     * 保存关联信息
     * @param categoryBrandRelation
     */
    @Override
    public void saveCategoryBrandRelationEntity(CategoryBrandRelationEntity categoryBrandRelation) {
        // 设置品牌名
        BrandEntity brandEntity = brandService.getById(categoryBrandRelation.getBrandId());
        if (brandEntity !=null){
            categoryBrandRelation.setBrandName(brandEntity.getName());
        }
        // 设置分类名
        CategoryEntity categoryEntity = categoryService.getById(categoryBrandRelation.getCatelogId());
        if (categoryEntity !=null){
            categoryBrandRelation.setCatelogName(categoryEntity.getName());
        }
        // 查询是否已经关联
        CategoryBrandRelationEntity one = this.getOne(new LambdaQueryWrapper<CategoryBrandRelationEntity>().
                eq(CategoryBrandRelationEntity::getBrandId, categoryBrandRelation.getBrandId())
                .eq(CategoryBrandRelationEntity::getCatelogId, categoryBrandRelation.getCatelogId()));

        if (one !=null){
            throw new RRException("该信息已经存在");
        }

        this.save(categoryBrandRelation);
    }

    /**
     * 根据分类id 查询当前关联的所有品牌
     * @param catId
     * @return
     */
    @Override
    public List<BrandVo> findBrands(Long catId) {
        List<CategoryBrandRelationEntity> list = this.list(new LambdaQueryWrapper<CategoryBrandRelationEntity>().eq(CategoryBrandRelationEntity::getCatelogId, catId));
        List<BrandVo> collect = list.stream().map(item -> {
            BrandVo brandVo = new BrandVo();
            BrandEntity brandEntity = brandService.getById(item.getBrandId());
            brandVo.setBrandId(brandEntity.getBrandId());
            brandVo.setBrandName(brandEntity.getName());
            return brandVo;
        }).collect(Collectors.toList());
        return collect;
    }

}